import Toast, { NewInstanceType } from './toast';

export interface InfoOptions {
  messages?: string;
  duration?: number;
  onSuccess?: () => void;
}

let toastInstance: NewInstanceType;

export default {
  info(options: InfoOptions | string) {
    if (typeof options === 'string') {
      options = {
        messages: options,
        duration: 2000,
      };
    } else {
      options = { ...{ duration: 2000 }, ...options };
    }
    toastInstance = Toast.newInstance(options);
    setTimeout(() => {
      if (typeof options === 'string') return;
      toastInstance && toastInstance.destroy?.();
      options.onSuccess && options.onSuccess?.();
    }, options.duration);
  },
};
